package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;

import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.BussTypeService;
import com.baichang.taoshunda.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;

/**
 * Created by Administrator on 2017/8/25 0025.
 */
@Controller
@RequestMapping(ControllerConstants.GOODS)
public class GoodsController extends WebBaseController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private BussTypeService bussTypeService;


    /**
     * 跳转商品列表
     */
    @RequestMapping("/findGoodsList")
    public String findGoodsList() {
        return "taoshunda/goods/goods";
    }

    /**
     * 每日特价的商品记录
     *
     * @return
     */
    @RequestMapping("/findDay")
    public String findSpecial() {
        return "taoshunda/goods/dayOffer";
    }

    /**
     * 需要上架的商品
     *
     * @return
     */
    @RequestMapping("/noUpperIndex")
    public String noUpperIndex() {
        return "taoshunda/goods/noUpper";
    }


    /**
     * 商品的查看订单交易记录
     */
    @RequestMapping("/jiaoYiDetail")
    public void jiaoYiDetail(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                int id = getInt(request, "id", 0);
                String cstart = getString(request, "cstart", "");
                //交易记录支付方式
                Integer jyjlzffs = getInt(request, "jyjlzffs", 0);
                return goodsService.getOrderListByGoodsId(nowPage, pageSize, id, cstart, jyjlzffs);

            }
        });
    }

    /**
     * 商品列表
     */
    @RequestMapping("/findGoods")
    public void findGoods(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                String goodsName = getString(request, "goodsName", "");
                int isSpecial = getInt(request, "isSpecial", -1);
                String bussName = getString(request, "bussName", "");
                Integer type = getInt(request, "type", -1);
                return goodsService.findGoods(nowPage, pageSize, goodsName, isSpecial, bussName, type);
            }
        });
    }


    /**
     * 商品的下架
     */
    @RequestMapping("/noUpper")
    public void noUpper(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                int id = getInt(request, "id", 0);
                Integer isSpecial = getInt(request, "special", 0);
                return goodsService.noUpper(id, isSpecial);
            }
        });
    }

    /**
     * 查询每日特价的商品
     */
    @RequestMapping("/queryDayOffer")
    public void queryDayOffer(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                String goodsName = getString(request, "goodsName", "");
                return goodsService.queryDayOffer(nowPage, pageSize, goodsName);
            }
        });
    }
//
//    /**
//     * 同意商品成为特价商品
//     */
//    @RequestMapping("/agreeSpecial")
//    public void agreeSpecial(HttpServletRequest request, HttpServletResponse response) {
//        init(request, response, new IController() {
//            @Override
//            public Object initData(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
//                int id = getInt(request, "id", 0);
//                int isSpecial = getInt(request, "isSpecial", 0);
//                try {
//                    return goodsService.agreeSpecial(id, isSpecial);
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
//                return null;
//            }
//        });
//    }
//
//    /**
//     * 拒绝特价商品申请  disagreeSpecial
//     *
//     * @param request
//     * @param response
//     */
//
//    @RequestMapping("/disagreeSpecial")
//    public void disagreeSpecial(HttpServletRequest request, HttpServletResponse response) {
//        init(request, response, new IController() {
//            @Override
//            public Object initData(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
//                int id = getInt(request, "id", 0);
//                return goodsService.disagreeSpecial(id);
//            }
//        });
//    }
//
//    /**
//     * 取消特价商品  removeSpecial   拒绝特价商品申请
//     *
//     * @param request
//     * @param response
//     */
//    @RequestMapping("/removeSpecial")
//    public void removeSpecial(HttpServletRequest request, HttpServletResponse response) {
//        init(request, response, new IController() {
//            @Override
//            public Object initData(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
//                int id = getInt(request, "id", 0);
//                return goodsService.removeSpecial(id);
//            }
//        });
//    }

    /**
     * 查询下架的商品查询列表
     */
    @RequestMapping("/noUpperList")
    public void noUpperList(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                String goodsName = getString(request, "goodsName", "");
                String busName = getString(request, "busName", "");
                return goodsService.noUpperList(nowPage, pageSize, goodsName, busName);
            }
        });
    }

    /**
     * 商品的上架
     */
    @RequestMapping("/yesUpper")
    public void yesUpper(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                int id = getInt(request, "id", 0);
                return goodsService.yesUpper(id);
            }
        });
    }

    /**
     * 删除商品
     */
    @RequestMapping("/deleteGoodsById")
    public void deleteGoodsById(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                int id = getInt(request, "id", 0);
                return goodsService.deleteGoodsById(id);
            }
        });
    }

    /**
     * 查询商品分类
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryGoodsType")
    public void queryGoodsType(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                return bussTypeService.queryGoodsType();
            }
        });
    }

    /**
     * 商家下的商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessGoods")
    public void businessGoods(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String goodsName = getString(request, "goodsName", "");
                return goodsService.businessGoods(id, goodsName, nowPage, pageSize);
            }
        });
    }
}
